// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

import SodaEventSoda from "../soda/SodaEventSoda";
import SodaCommon from "../soda/SodaCommon";
import { CodeNum } from "../soda/SodaCode";



@ccclass
export default class BindPhone extends cc.Component {

    @property(cc.Node)
    private btnCancle: cc.Node = null;

    @property(cc.Node)
    private btnSure: cc.Node = null;

    @property(cc.EditBox)
    private editPhone: cc.EditBox = null;

    private phoneNum: string = "";


    // onLoad () {}

    start() {
        this.phoneNum = SodaCommon.getUserPhoneNum();
        this.editPhone.string = this.phoneNum;
        let G = window['G'];
        let comHandler = new cc.Component.EventHandler();
        comHandler.target = this.node;
        comHandler.handler = 'onEditDidEnded';
        comHandler.component = 'BindPhone';
        this.editPhone.textChanged.push(comHandler);

        this.btnCancle.on('click', () => {
            F.Ui.closeUI(this.node);
        });

        this.btnSure.on('click', () => {
            SodaCommon.userExchange(this.phoneNum).then((res) => {
                SodaCommon.vibrateLong();
                if (res.code === CodeNum.CD_OK) {
                    SodaEventSoda.emit(SodaEventSoda.USER_EXCHANGE_SUCC, res);
                } else {
                    SodaEventSoda.emit(SodaEventSoda.ERROR, res);
                }
            });
        });

    }

    onEditDidEnded(text: string) {
        this.phoneNum = text;
    }

    // update (dt) {}
}
